from  selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from day1.page_locators.login_page_locator import LoginPageLocator  as LL
from day1.common.base_page import BasePage

class LoginPage(BasePage):

    def __init__(self, driver):
        super().__init__(driver)

    # def __init__(self, driver):
        # self.driver = webdriver.Chrome()
        # self.driver = driver

    # 封装  大封装 小封装

    # # 元素定位方式 属性
    # username_loctator = (By.NAME, 'username') # 脱外套
    # password_loctator = (By.NAME, 'password')
    # loginbtn_loctator = (By.CLASS_NAME, 'us_Submit')
    # errmsg_locator = (By.XPATH, '//*[contains(text(),"用户名或密码错误")]')
    # logout_locator = (By.LINK_TEXT, '退出')

    def login(self, username, password):
        # WebDriverWait(self.driver,10,1).until(EC.visibility_of_all_elements_located(LL.username_loctator))
        # self.driver.find_element(*LL.username_loctator).send_keys(username)
        # self.driver.find_element(*LL.password_loctator).send_keys(password)
        # self.driver.find_element(*LL.loginbtn_loctator).click()
        # self.wait_element_is_visible(LL.username_loctator,doc)
        self.input_text(LL.username_loctator, username, "登录模块_登录页面_用户名输入")
        self.input_text(LL.password_loctator, password, "登录模块_登录页面_密码输入")
        self.click_element(LL.loginbtn_loctator, "登录模块_登录页面_登录按钮点击")

    def get_err_msg_from_login_mode(self):
        """
        通过获取页面的报错信息来进行断言
        :return:
        """
        try:
            WebDriverWait(self.driver, 10, 1).until(EC.visibility_of_all_elements_located(LL.errmsg_locator))
            return True
        except:
            return False

    def is_visible_logout(self):
        """
        判断退出按钮是否可以正常显示
        :return:
        """
        try:
            WebDriverWait(self.driver, 10, 1).until(EC.visibility_of_all_elements_located(LL.logout_locator))
            return True
        except:
            return False